<?php
namespace app\api\logic;
use services\WxBizDataCryptService;
use services\WxService;
use think\Db;
use think\Request;
use think\Exception;
use think\facade\Cookie;
use think\facade\Session;
class User extends Base
{
    public $askforleave_status = ['待审核','同意','不同意'];
    /**
     *  用户头像，昵称更新 
     */
    public function userInfoUpdate($input){
        $uid = isset($input['token_user_id']) ? $input['token_user_id']:0;
        $nickname = isset($input['nickname']) ? trim($input['nickname']):'';
        $user_type = isset($input['user_type']) ? trim($input['user_type']):1;
        $avatar = isset($input['avatar']) ? trim($input['avatar']):'';
        if (!$uid || !$nickname || !$avatar){
            $this->return_data['Msg'] = " 参数错误";
            return $this->return_data;
        }
        $data['nickname']=$nickname;
        $data['user_type']=$user_type;
        $data['avatar']=$avatar;
        $data['update_time']=now_time();
        $res = Db::name('user')->where('id','=',$uid)->update($data);
        if (!$res){
            $this->return_data['Msg'] = " 更新信息失败";
            return $this->return_data;
        }
        $this->return_data['Code'] = 0;
        $this->return_data['Msg'] = "更新成功";
        return $this->return_data;
    }
    /**
     *  用户phone更新 
     */
    public function userPhoneUpdate($input){
        $uid = isset($input['user_id']) ? $input['user_id']:0;
        $encryptedData = isset($input['encryptedData'])?trim($input['encryptedData']):'';
        $iv = isset($input['iv'])?trim($input['iv']):'';
        if (!$uid || !$iv || !$encryptedData){
            $this->return_data['Msg'] = " 参数错误";
            return $this->return_data;
        }
        $appid = config('appid');
        $appsecret = config('appsecret');
        $code = $input['code'];
        $WxService = new WxService(['appid'=>$appid,'appsecret'=>$appsecret]);
        $tokenArr = $WxService->getSessionToken(["code"=>$code]);
        if(!isset($tokenArr['openid'])){
            $this->return_data['Msg'] = "授权失败";
            return $this->return_data;
        }
        //判断微信用户信息是否获取到
        $openid = $tokenArr['openid'];
        $session_key = $tokenArr['session_key'];
        $wxcryptService = new WxBizDataCryptService($appid,$session_key);
        $newData=array();
        $wxcryptService->decryptData($encryptedData,$iv,$newData);
        $nd = json_decode($newData);
        $phone = $nd->phoneNumber;
        if(!isset($phone)){
            $this->return_data['Msg'] = "无授权手机号";
            return $this->return_data;
        }
        $data['phone']=$phone;
        $data['update_time']=now_time();
        $res = Db::name('user')->where('id','=',$uid)->update($data);
        if (!$res){
            $this->return_data['Msg'] = " 更新信息失败";
            return $this->return_data;
        }
        $this->return_data['Code'] = 0;
        $this->return_data['Msg'] = "更新成功";
        $this->return_data['Data'] = ['phone'=>$phone];
        return $this->return_data;
    }
    public function isPass($input){
        $uid = isset($input['user_id']) ? $input['user_id']:0;
        if (!$uid){
            $this->return_data['Msg'] = " 参数错误";
            return $this->return_data;
        }
        $res = Db::name('supplier_apply')->where('uid','=',$uid)->find();
        if (!$res){
            $this->return_data['Msg'] = "无此申请";
            return $this->return_data;
        }
        $this->return_data['Code'] = 0;
        $this->return_data['Msg'] = "成功";
        $this->return_data['Data'] = $res;
        return $this->return_data;
    }
    public function userInfo($input){
        $uid = isset($input['user_id']) ? $input['user_id']:0;
        if (!$uid){
            $this->return_data['Msg'] = " 参数错误";
            return $this->return_data;
        }
        $user = Db::name('user')->where([['id','=',$uid]])->find();
        if (!$user){
            $this->return_data['Msg'] = " 无该用户";
            return $this->return_data;
        }
        if ($user['photo']) {
            $user['photo'] = config('upload_domain').$user['photo'];
        }
        $user['is_zxs'] = Db::name('store')->where('id',$user['sid'])->value('is_zxs');
        $user['createdate'] = date('Y-m-d',$user['create_time']);
        if ($user['is_teacher']==1) {
            $teacher = Db::name('teacher')->where(['user_id'=>$user['id']])->field('id,name,avatar')->find();
            if (!$teacher) {
                $this->return_data['Msg'] = " 尚未添加老师信息，请联系管理员";
                return $this->return_data;
            }
            $user['teacher_avatar'] = config('upload_domain').$teacher['avatar'];
            $user['teacher_name'] = $teacher['name'];
            $user['teacher_classes'] = Db::name('classes')->where(['master_teacher'=>$teacher['id']])->value('group_concat(name) as classes');
        } else {
            $user['teacher_name'] = '';
            $user['teacher_avatar'] = '';
            $user['teacher_classes'] = '';
        }
        $this->return_data['Code'] = 0;
        $this->return_data['Msg'] = "成功";
        $this->return_data['Data'] = $user;
        return $this->return_data;
    }

    public function editUserInfo($input)
    {
        $uid = isset($input['user_id']) ? $input['user_id']:0;
        $name = isset($input['name']) ? htmlentities($input['name']):'';
        $mother_name = isset($input['mother_name']) ? htmlentities($input['mother_name']):'';
        $father_name = isset($input['father_name']) ? htmlentities($input['father_name']):'';
        $photo = isset($input['photo']) ? htmlentities($input['photo']):'';
        if (!$uid || !$name || !$mother_name || !$father_name || !$photo){
            $this->return_data['Msg'] = " 参数错误";
            return $this->return_data;
        }
        $user = Db::name('user')->where([['id','=',$uid]])->find();
        if (!$user) {
            $this->return_data['Msg'] = "不存在该用户";
            return $this->return_data;
        }
        $data = [
            'name' => $name,
            'mother_name' => $mother_name,
            'father_name' => $father_name,
            'photo' => $photo,
            'update_time' => time(),
        ];
        $is_update = Db::name('user')->where('id',$uid)->update($data);
        if (!$is_update) {
            $this->return_data['Msg'] = "更新失败";
            return $this->return_data;
        }
        $this->return_data['Code'] = 0;
        $this->return_data['Msg'] = "成功";
        $this->return_data['Data'] = $user;
        return $this->return_data;
    }


    // 课时日历
    public function calendar($input)
    {
        $uid = isset($input['user_id']) ? $input['user_id']:0;
        if (!$uid){
            $this->return_data['Msg'] = " 参数错误";
            return $this->return_data;
        }
        $user = Db::name('user')->where([['id','=',$uid]])->find();
        if (!$user) {
            $this->return_data['Msg'] = "不存在该用户";
            return $this->return_data;
        }
        $data = [];
        $classtime = Db::name('user_course')->alias('uc')->leftJoin('classtime ct','uc.course_id=ct.course_id')->field('ct.*')->where('uc.user_id='.$uid.' and uc.status=2 and ct.status=1')->order('begin_time ASC')->select();
        if ($classtime) {
            foreach ($classtime as $key => $value) {
                $info = '';
                $status = Db::name('user_signin')->where('classtime_id='.$value['id'].' and user_id='.$uid)->value('status');
                if ($status>0) {
                    switch ($status) {
                        case 1:
                            $info = '正常';
                            break;
                        case 2:
                            $info = '迟到';
                            break;
                        case 3:
                            $info = '请假';
                            break;
                        case 4:
                            $info = '缺课';
                            break;
                    }
                }
                $data[$key]['date'] = date('Y-m-d',$value['begin_time']);
                $data[$key]['info'] = $info;
                $data[$key]['data'] = ['custom'=>'自定义信息','name'=>'自定义消息头','color'=>'#E7402E'];
            }
        }
        $sum = Db::name('user_course')->alias('uc')->where('uc.user_id='.$uid.' and uc.status=2')->value('sum(classtime_surplus)');
        if (!$sum) {
            $sum = 0;
        }
        // if (isset($arr['count']) && $arr['count']) {
        //     $done = Db::name('user_signin')->where('classtime_id in ('.$arr['ctids'].') and user_id='.$uid.' and status=1')->value('count(id)');
        //     $sum = $arr['count']-$done;
        // }
        $this->return_data["Code"] = 0;
        $this->return_data["Data"] = $data;
        $this->return_data["num"] = $sum;
        return $this->return_data;
    }


    // 课程列表
    public function dayCourse($input)
    {
        $uid = isset($input['user_id']) ? $input['user_id']:0;
        $date = isset($input['date']) ? $input['date']:date('Y-m-d');
        if (!$uid){
            $this->return_data['Msg'] = " 参数错误";
            return $this->return_data;
        }
        $user = Db::name('user')->where([['id','=',$uid]])->find();
        if (!$user) {
            $this->return_data['Msg'] = "不存在该用户";
            return $this->return_data;
        }
        $date_begin = strtotime($date);
        $date_end = $date_begin+86400;
        $data = [];
        $classtime = Db::name('user_course')->alias('uc')
                ->leftJoin('course c','c.id=uc.course_id')
                ->leftJoin('classtime ct','c.id=ct.course_id')
                ->field('c.id,c.name,c.sid,c.classes_name,c.teacher_name,c.classroom_name,c.address,c.summary,ct.begin_time,ct.end_time,ct.no,ct.id as ctid,uc.classtime_surplus')
                ->where('uc.user_id='.$uid.' and uc.status=2 and ct.begin_time>'.$date_begin.' and ct.begin_time<'.$date_end.' and c.status=1 and ct.status=1')
                ->order('ct.begin_time ASC')
                ->select();
        if (isset($classtime[0]['id']) && $classtime[0]['id']) {
            foreach ($classtime as $key => &$value) {
                $store_name = Db::name('store')->where('id',$value['sid'])->value('name');
                $value['classes_name'] = $store_name.$value['classes_name'];
                $value['begin_time'] = date('H:i',$value['begin_time']);
                $value['end_time'] = date('H:i',$value['end_time']);
                $user_signin = Db::name('user_signin')->where(['classtime_id'=>$value['ctid'],'user_id'=>$uid])->find();
                $value['is_sign'] = 0;// 未完课
                if ($user_signin && $user_signin['status']==1) {
                    $value['is_sign'] = 1;// 正常
                } elseif ($user_signin && $user_signin['status']==3) {
                    $value['is_sign'] = 3;// 异常
                } elseif ($user_signin && $user_signin['status']>1) {
                    $value['is_sign'] = 2;// 异常
                }
                
            }
        } else {
            $classtime = [];
        }
        $this->return_data["Code"] = 0;
        $this->return_data["Data"] = $classtime;
        return $this->return_data;
    }


    // 成长记录列表
    public function growupList($input)
    {
        $uid = isset($input['user_id']) ? $input['user_id']:0;
        $page = isset($input['page']) ? $input['page']:1;
        $limit = isset($input['limit']) ? $input['limit']:10;
        if (!$uid){
            $this->return_data['Msg'] = " 参数错误";
            return $this->return_data;
        }
        $user = Db::name('user')->where([['id','=',$uid]])->find();
        if (!$user) {
            $this->return_data['Msg'] = "不存在该用户";
            return $this->return_data;
        }
        $user_comment = Db::name('user_comment')->where(['user_id'=>$uid])->order('createtime DESC')->page($page,$limit)->select();
        if ($user_comment) {
            foreach ($user_comment as $key => &$value) {
                $value['createtime'] = date('Y-m-d',$value['createtime']);
            }
        }
        $this->return_data["Code"] = 0;
        $this->return_data["Data"] = $user_comment;
        return $this->return_data;
    }


    // 成长记录详细
    public function growupInfo($input)
    {
        $uid = isset($input['user_id']) ? $input['user_id']:0;
        $id = isset($input['id']) ? $input['id']:0;
        if (!$uid || !$id) {
            $this->return_data['Msg'] = " 参数错误";
            return $this->return_data;
        }
        $user = Db::name('user')->where([['id','=',$uid]])->find();
        if (!$user) {
            $this->return_data['Msg'] = "不存在该用户";
            return $this->return_data;
        }
        $user_comment = Db::name('user_comment')->where(['id'=>$id])->find();
        if (!$user_comment) {
            $this->return_data['Msg'] = "不存在该成长记录";
            return $this->return_data;
        }
        $user_comment['createtime'] = date('Y-m-d',$user_comment['createtime']);
        $this->return_data["Code"] = 0;
        $this->return_data["Data"] = $user_comment;
        return $this->return_data;
    }


    // 学员风采列表
    public function studentStyle($input)
    {
        $uid = isset($input['user_id']) ? $input['user_id']:0;
        $page = isset($input['page']) ? $input['page']:1;
        $limit = isset($input['limit']) ? $input['limit']:10;
        $sid = isset($input['sid'])?intval($input['sid']):0; 
        if (!$sid){
            $this->return_data['Msg'] = "无参数";
            return $this->return_data;
        }
        if (!$uid){
            $this->return_data['Msg'] = " 参数错误";
            return $this->return_data;
        }
        $user = Db::name('user')->where([['id','=',$uid]])->find();
        if (!$user) {
            $this->return_data['Msg'] = "不存在该用户";
            return $this->return_data;
        }
        $student_style = Db::name('student_style')->where(['status'=>1,'sid'=>$sid])->order('createtime DESC')->page($page,$limit)->select();
        if ($student_style) {
            foreach ($student_style as $key => &$value) {
                $value['img'] = Config('upload_domain').$value['img'];
                $value['createtime'] = date('Y-m-d',$value['createtime']);
            }
        }
        $this->return_data["Code"] = 0;
        $this->return_data["Data"] = $student_style;
        return $this->return_data;
    }


    // 学员风采详细
    public function studentStyleInfo($input)
    {
        $uid = isset($input['user_id']) ? $input['user_id']:0;
        $id = isset($input['id']) ? $input['id']:0;
        if (!$uid || !$id) {
            $this->return_data['Msg'] = " 参数错误";
            return $this->return_data;
        }
        $user = Db::name('user')->where([['id','=',$uid]])->find();
        if (!$user) {
            $this->return_data['Msg'] = "不存在该用户";
            return $this->return_data;
        }
        $student_style = Db::name('student_style')->where(['id'=>$id])->find();
        if (!$student_style) {
            $this->return_data['Msg'] = "不存在该成长记录";
            return $this->return_data;
        }
        $student_style['contents'] = str_replace('<img', '<img width="100%"', $student_style['contents']);
        $student_style['createtime'] = date('Y-m-d',$student_style['createtime']);
        $this->return_data["Code"] = 0;
        $this->return_data["Data"] = $student_style;
        return $this->return_data;
    }


    // 我的课程
    public function myCourse($input)
    {
        $uid = isset($input['user_id']) ? $input['user_id']:0;
        if (!$uid) {
            $this->return_data['Msg'] = " 参数错误";
            return $this->return_data;
        }
        $user_course = Db::name('user_course')->alias('uc')->leftJoin('course c','c.id=uc.course_id')->field('c.id,c.pic,uc.status,uc.course_id')->where([['uc.user_id','=',$uid],['uc.status','>',0]])->group('uc.course_id')->select();
        if ($user_course) {
            foreach ($user_course as $key => &$value) {
                $value['pic'] = config('upload_domain').$value['pic'];
            }
        }
        $this->return_data["Code"] = 0;
        $this->return_data["Data"] = $user_course;
        return $this->return_data;
    }


    // 请假
    public function askForLeave($input)
    {
        $uid = isset($input['user_id']) ? $input['user_id']:0;
        $sid = isset($input['sid']) ? $input['sid']:0;
        $classtime_id = isset($input['classtime_id']) ? $input['classtime_id']:0;
        $type = isset($input['type']) ? trim($input['type']):'';
        $reason = isset($input['reason']) ? trim($input['reason']):'';
        if (!$uid || !$classtime_id) {
            $this->return_data['Msg'] = " 缺少参数";
            return $this->return_data;
        }
        if (!$type) {
            $this->return_data['Msg'] = " 请选择请假原因";
            return $this->return_data;
        }
        if (!$reason) {
            $this->return_data['Msg'] = " 请输入请假理由";
            return $this->return_data;
        }
        $user = Db::name('user')->where([['id','=',$uid]])->find();
        if (!$user) {
            $this->return_data['Msg'] = "不存在该用户";
            return $this->return_data;
        }
        $course = Db::name('classtime')->alias('ct')->leftJoin('course c','c.id=ct.course_id')->where('ct.id='.$classtime_id)->field('c.teacher_id,c.teacher_name,ct.begin_time,ct.end_time,c.name')->find();
        if (!$course) {
            $this->return_data['Msg'] = "课程数据有误";
            return $this->return_data;
        }
        $askforleave = Db::name('askforleave')->where(['user_id'=>$uid,'classtime_id'=>$classtime_id])->find();
        if ($askforleave && in_array($askforleave['status'], [0,1])) {
            $this->return_data['Msg'] = "您已请过假了";
            return $this->return_data;
        }
        Db::startTrans();
        try {
            $data['user_id'] = $uid;
            $data['classtime_id'] = $classtime_id;
            $data['teacher_id'] = $course['teacher_id'];
            $data['type'] = htmlentities($type);
            $data['reason'] = htmlentities($reason);
            $data['createtime'] = time();
            $is_insert = Db::name('askforleave')->insertGetId($data);
            if (!$is_insert) {
                Db::rollBack();
                $this->return_data['Msg'] = "请假出错";
                return $this->return_data;
            }

            // 请假通知
            $template_id = 'NfBxbtgH8eYL7liXJDRmRgmNAaT8FbbQooCgkYsjYoQ';
            $openid = Db::name('teacher')->alias('t')->leftJoin('user u','u.id=t.user_id')->where('t.id='.$course['teacher_id'])->value('u.fwhopenid');
            $page = 'orderPackages/pages/admin/leave?notice=1&sid='.$sid;
            $data = [
                'first' => ['value' => $course['teacher_name'].'老师，你有一条学员请假通知'],
                'keyword1' => ['value' => $user['name']],
                'keyword2' => ['value' => date('Y-m-d H:i',$course['begin_time'])],
                'keyword3' => ['value' => $course['name']],
                'remark' => ['value' => $reason],
            ];
            write_log(json_encode($data,1));
            $PublicsLogic = new \app\api\logic\Publics();
            $res = $PublicsLogic->send_template_message($template_id, $page, $data, $openid);
            Db::commit();
            $this->return_data["Code"] = 0;
            $this->return_data["Msg"] = '请假成功，等待老师批准';
            return $this->return_data;
        } catch (\Exception $e) {
            Db::rollBack();
            $this->return_data['Msg'] = "请假出错";
            return $this->return_data;
        }
    }


    // 请假列表
    public function askForLeaveList($input)
    {
        $uid = isset($input['user_id']) ? $input['user_id']:0;
        $sid = isset($input['sid']) ? $input['sid']:0;
        if (!$uid || !$sid) {
            $this->return_data['Msg'] = " 缺少参数";
            return $this->return_data;
        }
        $user = Db::name('user')->where(['sid'=>$sid,'id'=>$uid])->find();
        if (!$user) {
            $this->return_data['Msg'] = " 不存在该用户";
            return $this->return_data;
        }
        $askforleave = Db::name('askforleave')->where(['user_id'=>$uid])->order('id desc')->select();
        $data = [];
        if ($askforleave) {
            foreach ($askforleave as $key => $value) {
                $data[$key] = $value;
                $data[$key]['student_name'] = $user['name'];
                $data[$key]['course_name'] = '';
                $data[$key]['course_date'] = '';
                $course = Db::name('classtime')->alias('ct')->leftJoin('course c','c.id=ct.course_id')->field('c.name,ct.begin_time')->where(['ct.id'=>$value['classtime_id']])->find();
                if ($course) {
                    $data[$key]['course_name'] = $course['name'];
                    $data[$key]['course_date'] = date('Y-m-d H:i:s',$course['begin_time']);
                }
                $data[$key]['createtime'] = date('Y-m-d H:i:s',$value['createtime']);
                $data[$key]['status'] = $this->askforleave_status[$value['status']];
                $makeuplesson = Db::name('makeuplesson')->where([['askforleave_id','=',$value['id']]])->find();
                $data[$key]['has_makeuplesson'] = $makeuplesson?1:0;
            }
        }
        $this->return_data["Code"] = 0;
        $this->return_data["Data"] = $data;
        return $this->return_data;
    }

    // 补课申请
    public function makeUpLessons($input)
    {
        $uid = isset($input['user_id']) ? $input['user_id']:0;
        $sid = isset($input['sid']) ? $input['sid']:0;
        $id = isset($input['id']) ? $input['id']:0;
        $makeup_time = isset($input['makeup_time']) ? strtotime($input['makeup_time']):0;
        if (!$uid || !$id) {
            $this->return_data['Msg'] = " 缺少参数";
            return $this->return_data;
        }
        if (!$makeup_time) {
            $this->return_data['Msg'] = " 选择补课时间";
            return $this->return_data;
        }
        $user = Db::name('user')->where([['id','=',$uid]])->find();
        if (!$user) {
            $this->return_data['Msg'] = "不存在该用户";
            return $this->return_data;
        }
        $askforleave = Db::name('askforleave')->where(['id'=>$id,'user_id'=>$uid])->find();
        if (!$askforleave) {
            $this->return_data['Msg'] = " 找不到对应的请假申请";
            return $this->return_data;
        }
        $makeuplesson = Db::name('makeuplesson')->where(['askforleave_id'=>$id])->find();
        if (isset($makeuplesson['status']) && $makeuplesson['status']==0) {
            $this->return_data['Msg'] = " 补课申请已提交，请等待审核结果";
            return $this->return_data;
        }
        $course = Db::name('classtime')->alias('ct')->leftJoin('course c','c.id=ct.course_id')->where('ct.id='.$askforleave['classtime_id'])->field('c.teacher_id,c.teacher_name,ct.begin_time,ct.end_time,c.name')->find();
        if (!$course) {
            $this->return_data['Msg'] = "课程数据有误";
            return $this->return_data;
        }
        Db::startTrans();
        try {
            $data['user_id'] = $askforleave['user_id'];
            $data['classtime_id'] = $askforleave['classtime_id'];
            $data['teacher_id'] = $askforleave['teacher_id'];
            $data['askforleave_id'] = $id;
            $data['askforleave_time'] = $askforleave['createtime'];
            $data['makeup_time'] = $makeup_time;
            $data['createtime'] = time();
            $is_insert = Db::name('makeuplesson')->insertGetId($data);
            if (!$is_insert) {
                Db::rollBack();
                $this->return_data['Msg'] = "补课申请出错";
                return $this->return_data;
            }

            // 补课通知
            $template_id = 'q2a9bLuyB1hfLYerOMNYbQQRW3gnrnzT6aUIfGRG-rk';
            $teacher = Db::name('teacher')->alias('t')->leftJoin('user u','u.id=t.user_id')->where('t.id='.$askforleave['teacher_id'])->field('u.fwhopenid,t.name')->find();
            if ($teacher['fwhopenid']) {
                $openid = $teacher['fwhopenid'];
                $page = 'orderPackages/pages/myChild/myLessons?notice=1&sid='.$sid;
                $data = [
                    'first' => ['value' => $teacher['name'].'老师，你的学生发起了补课申请'],
                    'keyword1' => ['value' => $askforleave['type']],
                    'keyword2' => ['value' => date('Y-m-d H:i',$course['begin_time'])],
                    'keyword3' => ['value' => $askforleave['reason']],
                    'keyword4' => ['value' => date('Y-m-d H:i',$makeup_time)],
                    'remark' => ['value' => '请尽快处理！'],
                ];
                write_log(json_encode($data));
                $PublicsLogic = new \app\api\logic\Publics();
                $res = $PublicsLogic->send_template_message($template_id, $page, $data, $openid);
            }
            Db::commit();
            $this->return_data["Code"] = 0;
            $this->return_data["Msg"] = '补课申请成功，等待老师批准';
            return $this->return_data;
        } catch (\Exception $e) {
            Db::rollBack();
            $this->return_data['Msg'] = "补课申请出错";
            return $this->return_data;
        }
    }


    // 补课列表
    public function makeUpLessonsList($input)
    {
        $uid = isset($input['user_id']) ? $input['user_id']:0;
        $sid = isset($input['sid']) ? $input['sid']:0;
        if (!$uid || !$sid) {
            $this->return_data['Msg'] = " 缺少参数";
            return $this->return_data;
        }
        $user = Db::name('user')->where(['sid'=>$sid,'id'=>$uid])->find();
        if (!$user) {
            $this->return_data['Msg'] = " 不存在该用户";
            return $this->return_data;
        }
        $makeuplesson = Db::name('makeuplesson')->field('*')->where(['user_id'=>$uid])->order('id desc')->select();
        $data = [];
        if ($makeuplesson) {
            foreach ($makeuplesson as $key => $value) {
                $data[$key] = $value;
                $data[$key]['student_name'] = $user['name'];
                $data[$key]['course_name'] = '';
                $data[$key]['teacher_name'] = '';
                $data[$key]['classes_name'] = '';
                $data[$key]['classroom_name'] = '';
                $data[$key]['address'] = '';
                $data[$key]['course_date'] = '';
                $course = Db::name('classtime')->alias('ct')->leftJoin('course c','c.id=ct.course_id')->field('c.name,ct.begin_time,ct.end_time,c.address,c.teacher_name,c.classes_name,c.classroom_name')->where(['ct.id'=>$value['classtime_id']])->find();
                if ($course) {
                    $data[$key]['course_name'] = $course['name'];
                    $data[$key]['teacher_name'] = $course['teacher_name'];
                    $data[$key]['classes_name'] = $course['classes_name'];
                    $data[$key]['classroom_name'] = $course['classroom_name'];
                    $data[$key]['address'] = $course['address'];
                    $data[$key]['course_date'] = date('Y-m-d H:i',$course['begin_time']).'-'.date('H:i',$course['end_time']);
                    $data[$key]['status'] = $this->askforleave_status[$value['status']];
                }
                $data[$key]['createtime'] = date('Y-m-d H:i:s',$value['createtime']);
                $data[$key]['askforleave_time'] = date('Y-m-d H:i:s',$value['askforleave_time']);
                $data[$key]['makeup_time'] = date('Y-m-d H:i:s',$value['makeup_time']);
                $makeuplesson = Db::name('makeuplesson')->where([['askforleave_id','=',$value['askforleave_id']],['status','=',1]])->find();
                $data[$key]['has_makeuplesson'] = $makeuplesson?1:0;
            }
        }
        $this->return_data["Code"] = 0;
        $this->return_data["Data"] = $data;
        return $this->return_data;
    }
}

